class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int min_in_left = 0;
        int cur_sum = 0;
        int result = INT_MIN;
        for(int i : nums)
        {
            cur_sum += i;
            result = max(result, cur_sum - min_in_left);
            min_in_left = min(min_in_left, cur_sum);
        }
        return result;
    }
};

// much cooler
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int res = nums[0], sum = 0;
        for(int i : nums)
        {
            sum += i;
            res = max(sum, res);
            sum = max(0, sum);
        }
        return res;
    }
};
